﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SPSAndong.Handle
{

    /// <summary>
    /// 通讯控制-RS485
    /// 普通指令格式
    /// 指令格式：（模块默认是 9600 波特率 8 位数据位 1 位停止位 无校验）
    /// 开始字+ID+字节个数+指令字+数据流+校验码
    /// 指令说明：
    /// 开始字：0xF0
    /// ID：通过软件设置的本机地址编码；编码范围 0-255。（0 为广播地址）
    /// 字节个数：本条指令一共有多少个字节
    /// 指令字：控制指令码
    /// 数据流：数据内容
    /// 校验码：字节和
    /// </summary>
    public class VoiceCom
    {
        /// <summary>
        /// 播放语音指令：
        /// 请求：F0 ID 06 04 DATA JY 响应：F0 ID 06 04 01 JY
        /// 请求：F0 01 06 04 02 FD 响应：F0 01 06 04 01 FC
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public static byte[] Play(int index)
        {
            byte[] res = new byte[6];
            res[0]= 0xF0;
            res[1] = 0x01;
            res[2] = 0x06;
            res[3] = 0x04;
            res[4] = (byte)index;
            res[5] = AddByte(res,5);
            return res;
        }
        /// <summary>
        /// 增加音量
        /// 请求：F0 ID 05 05 JY 响应：F0 ID 06 05 01 JY
        /// 请求：F0 01 05 05 FB 响应：F0 01 06 05 01 FD
        /// </summary>
        /// <returns></returns>
        public static byte[] VoiceAdd()
        {
            byte[] res = new byte[5];
            res[0] = 0xF0;
            res[1] = 0x01;
            res[2] = 0x05;
            res[3] = 0x05;
            res[4] = AddByte(res, 4);
            return res;
        }
        /// <summary>
        /// 减小音量
        /// 请求：F0 ID 05 06 JY 响应：F0 ID 06 06 01 JY
        /// 请求：F0 01 05 06 FC 响应：F0 01 06 06 01 FE
        /// </summary>
        /// <returns></returns>
        public static byte[] VoiceReduce()
        {
            byte[] res = new byte[5];
            res[0] = 0xF0;
            res[1] = 0x01;
            res[2] = 0x05;
            res[3] = 0x06;
            res[4] = AddByte(res, 4);
            return res;
        }
        /// <summary>
        /// 下一曲
        /// 请求：F0 ID 05 07 JY 响应：F0 ID 06 07 01 JY
        /// 请求：F0 01 05 07 FD 响应：F0 01 06 07 01 FF
        /// </summary>
        /// <returns></returns>
        public static byte[] PlayNext()
        {
            byte[] res = new byte[5];
            res[0] = 0xF0;
            res[1] = 0x01;
            res[2] = 0x05;
            res[3] = 0x07;
            res[4] = AddByte(res, 4);
            return res;
        }
        /// <summary>
        /// 上一曲
        /// 请求：F0 ID 05 08 JY 响应：F0 ID 06 08 01 JY
        /// 请求：F0 01 05 08 FE 响应：F0 01 06 08 01 00
        /// </summary>
        /// <returns></returns>
        public static byte[] PlayPre()
        {
            byte[] res = new byte[5];
            res[0] = 0xF0;
            res[1] = 0x01;
            res[2] = 0x05;
            res[3] = 0x08;
            res[4] = AddByte(res, 4);
            return res;
        }
        /// <summary>
        /// 暂停
        /// 请求：F0 ID 05 09 JY 响应：F0 ID 06 09 01 JY
        /// 请求：F0 01 05 09 FF 响应：F0 01 06 09 01 01
        /// </summary>
        /// <returns></returns>
        public static byte[] Pause()
        {
            byte[] res = new byte[5];
            res[0] = 0xF0;
            res[1] = 0x01;
            res[2] = 0x05;
            res[3] = 0x09;
            res[4] = AddByte(res, 4);
            return res;
        }
        /// <summary>
        /// 停止播放，清空列表
        /// 请求：F0 ID 05 0A JY 响应：F0 ID 06 0A 01 JY
        /// 请求：F0 01 05 0A 00 响应：F0 01 06 0A 01 02
        /// </summary>
        /// <returns></returns>
        public static byte[] Stop()
        {
            byte[] res = new byte[5];
            res[0] = 0xF0;
            res[1] = 0x01;
            res[2] = 0x05;
            res[3] = 0x0A;
            res[4] = AddByte(res, 4);
            return res;
        }
        /// <summary>
        /// 循环播发
        /// 请求：F0 ID 06 0B DATA JY 响应：F0 ID 06 0B 01 JY
        /// 请求：F0 01 06 0B 02 04 响应：F0 01 06 0B 01 03
        /// </summary>
        /// <returns></returns>
        public static byte[] PlayLoop(int index)
        {
            byte[] res = new byte[6];
            res[0] = 0xF0;
            res[1] = 0x01;
            res[2] = 0x06;
            res[3] = 0x0B;
            res[4] = (byte)index;
            res[5] = AddByte(res, 5);
            return res;
        }
        /// <summary>
        /// 停止循环播放
        /// 请求：F0 ID 05 0C JY 响应：F0 ID 06 0C 01 JY
        /// 请求：F0 01 05 0C 02 响应：F0 01 06 0C 01 04
        /// </summary>
        /// <returns></returns>
        public static byte[] StopLoop()
        {
            byte[] res = new byte[5];
            res[0] = 0xF0;
            res[1] = 0x01;
            res[2] = 0x05;
            res[3] = 0x0C;
            res[4] = AddByte(res, 4);
            return res;
        }
        /// <summary>
        /// 字节相加
        /// </summary>
        /// <param name="src"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static byte AddByte(byte[] src,int len)
        {
            byte result = 0x00;
            for (int i = 0; i < len; i++)
                result += src[i];
            return result;
        }
    }
}
